#!/bin/bash
# sub-release
#
#{{IS_NOTE
#	Purpose:
#		
#	Description:
#		
#	History:
#		July 2007, Created by tomyeh
#}}IS_NOTE
#
#Copyright (C) 2007 Potix Corporation. All Rights Reserved.
#
#{{IS_RIGHT
#	This program is distributed under Lesser GPL Version 2.1 in the hope that
#	it will be useful, but WITHOUT ANY WARRANTY.
#}}IS_RIGHT
#

pkgnm=$1
target=$2

zipnm=Fusionchart
prjnm=fusionchart
demoprjnm=zkfusionchartdemo
releasenote=fusionchart-release-note

if [ -f build.setting.local ] ; then
	setting=build.setting.local
else
	setting=build.setting
fi
javadocdir=$(grep '^javadoc' $setting)
javadocdir=${javadocdir#javadoc=}
if [ "$javadocdir" = "" ] ; then
	echo javadoc must be specified in $setting
	exit 1
fi
javadocdir=${javadocdir/\~/$HOME}
if [ ! -e $javadocdir/$prjnm ] ; then
	echo "$javadocdir/$prjnm not available"
	exit 1
fi

if [ "$target" == "official" ] ; then
	ver=$(head -1 $prjnm/version)
	binnm=zk-$zipnm-bin-$ver
	srcnm=zk-$zipnm-src-$ver
	demonm=zk-$zipnm-demo-$ver
elif [ "$target" == "" ] || [ "$target" == "daily" ] ; then
	target=daily

	ver=$(head -1 $prjnm/version)
	binnm=zk-$zipnm-bin-$ver-$(date +%Y-%m-%d)
	srcnm=zk-$zipnm-src-$ver-$(date +%Y-%m-%d)
	demonm=zk-$zipnm-demo-$ver-$(date +%Y-%m-%d)
else
	echo "Usage: release [ official | daily ]"
	echo Default: daily
	exit 1
fi

rm -rf /tmp/zk
maindir="$(pwd)"

echo "Release $binnm $srcnm $demonm"
cd $maindir
mkdir -p /tmp/zk/$binnm/dist/lib
mkdir -p /tmp/zk/$binnm/dist/src
mkdir -p /tmp/zk/$binnm/javadoc
mkdir -p /tmp/zk/$binnm/doc

mkdir -p /tmp/zk/$srcnm/dist/lib
mkdir -p /tmp/zk/$srcnm/doc

mkdir -p /tmp/zk/$demonm/$demoprjnm/WebContent
mkdir -p /tmp/zk/$demonm/$demoprjnm/src
mkdir -p /tmp/zk/$demonm/doc

cp build* /tmp/zk/$srcnm
cp -rp bin /tmp/zk/$srcnm
cp -rp $prjnm /tmp/zk/$srcnm
cp -f dist/lib/$prjnm.jar /tmp/zk/$binnm/dist/lib
cp zkdoc/COPYING /tmp/zk/$binnm/doc
cp zkdoc/COPYING /tmp/zk/$srcnm/doc
cp zkdoc/COPYING /tmp/zk/$demonm/doc
cp zkdoc/$releasenote /tmp/zk/$binnm/doc/release-note
cp zkdoc/$releasenote /tmp/zk/$srcnm/doc/release-note
cp zkdoc/$releasenote /tmp/zk/$demonm/doc/release-note

if [ "$target" = "zuljsp" ] ; then
	mkdir -p /tmp/zk/$binnm/dist/WEB-INF/tld/jsp/zul
	cp -rp dist/export/WEB-INF/tld/jsp/zul/* /tmp/zk/$binnm/dist/WEB-INF/tld/jsp/zul
fi

#prepare source.jar
cd /tmp/zk/$srcnm
find -name .svn | xargs rm -rf
cd $prjnm
rm -rf codegen debug
jar cfM $prjnm-sources.jar -C src org
mv -f $prjnm-sources.jar /tmp/zk/$binnm/dist/src

#prepare javadoc
cd $javadocdir
cp -rp $prjnm/* /tmp/zk/$binnm/javadoc

#Copy and pack demo
cd $HOME/prj/fusionchart/trunk

cp -rp $demoprjnm/src/archive/* /tmp/zk/$demonm/$demoprjnm/WebContent
cp -rp $demoprjnm/src/* /tmp/zk/$demonm/$demoprjnm/src
cp -rp $demoprjnm/.project $demoprjnm/.classpath $demoprjnm/.settings /tmp/zk/$demonm/$demoprjnm/
cp dist/lib/$demoprjnm.war /tmp/zk/$demonm/$demoprjnm

cd /tmp/zk/$demonm/$demoprjnm
rm -rf src/archive
find -name .svn | xargs rm -rf
cd /tmp/zk
echo Output ${demonm}.zip
zip -qr9 ${demonm}.zip $demonm
rm -rf $demonm

#pack bin

cd /tmp/zk
echo Output ${binnm}.zip
zip -qr9 ${binnm}.zip $binnm

if [ "$target" == "official" ] ; then
	#Prepare maven repository
	function mvnrepo {
		echo "Create $1-$ver-bundle.jar"
		mkdir -p $1
		mv /tmp/zk/$binnm/dist/src/$1-sources.jar $1/$1-$ver-sources.jar
		sed -e "s/{version}/$ver/" $maindir/$1/pom.xml > $1/pom.xml
		cp $maindir/dist/lib/$1.jar $1/$1-$ver.jar
		chmod -R 755 $1/$1-$ver.jar
		jar cfM $1-$ver-bundle.jar -C $1 .
	}
	mkdir -p maven
	mkdir -p /tmp/maven
	cd maven
	mvnrepo $prjnm
	mv *-bundle.jar /tmp/maven
	cd ..
	rm -rf maven
fi
cd /tmp/zk
rm -rf $binnm

#pack src
cd /tmp/zk
echo "Output ${pwd}${srcnm}.zip"
zip -qr9 ${srcnm}.zip $srcnm
rm -rf $srcnm